﻿function LocalStorageManager(localstorage) {
    this.localStorage = localstorage;
}

LocalStorageManager.prototype = {
    pageLoad: function () {
        //非登录页面或非登录状态
        if ($('#loginForm').length == 0 || !this.isLogin()) {
            return;
        }
        //服务关闭再连接
        if (this.localStorage.serverShutdown) {
            return;
        }

        var now = new Date();
        var lt = new Date(this.localStorage.lastAccessTime);
        console.log('now:' + now.now() + '; lastAccessTime:' + lt.now() + '; offset-time:' + (now - lt));

        if (now - lt > 10 * 1000) {
            console.log('清除历史登录信息[' + this.getClientIP() + ']');
            this.clearLoginInfo();
        }
    },
    pageUnload: function () {

    },
    isLogin: function () {
        return this.localStorage.getItem('isLogin');
    },
    getClientIP: function () {
        return this.localStorage.getItem('clientIP');
    },
    clearLoginInfo: function () {
        this.localStorage.removeItem('isLogin');
        this.localStorage.removeItem('clientIP');
        this.localStorage.removeItem('lastAccessTime');
    },
    add: function (key, value) {
        this.localStorage.setItem(key, value);
    },
    updateLastAccessTime: function (lastAccessTime) {
        this.localStorage.lastAccessTime = lastAccessTime;
    },
    setServerShutdown: function () {
        this.localStorage.serverShutdown = true;
    },
    removeServerShutdown: function () {
        this.localStorage.removeItem('serverShutdown');
    }
}